/*完全版AI*/

var aiComplete = {};

//AI初始化
aiComplete.init = function(){
	aiComplete.headQueue = [];
	aiComplete.mostPoss = [[3,3],[6,3],[3,6],[6,6]];
	//console.log(aiComplete.mostPoss);
	aiComplete.mostPossCount = 4;
	aiComplete.board = Array(10);
	for (var i = 0; i < 10; i++) {
		aiComplete.board[i] = Array(10);
		for (var j = 0; j < 10; j++) {
			aiComplete.board[i][j] = 0;
		}
	}
	aiComplete.tmpBoard = aiComplete.board.concat();
}

/*随机布置飞机*/
aiComplete.creatFlights = function(){
	while (enemyFlightCount<3){
		var x = parseInt(Math.random()*10);
		var y = parseInt(Math.random()*10);
		var d = parseInt(Math.random()*4);
		while (!isLegal(x,y,d,enemyBoard,enemyFlightCount)){
			x = parseInt(Math.random()*10);
			y = parseInt(Math.random()*10);
			d = parseInt(Math.random()*4);
		}
		enemyFlight[enemyFlightCount][0] = x;
		enemyFlight[enemyFlightCount][1] = y;
		enemyFlight[enemyFlightCount][2] = d;
		console.log('x:'+x+' y:'+y+' d:'+d);
		for (var i = 0; i < 10; i++) {
			var xi = x + dirInc[d][i][0];
			var yi = y + dirInc[d][i][1];
			var indexi = yi*10 + xi;
			enemyBoard[xi][yi] = true;
		}
		enemyFlightCount++;
	}
}

/*接收轰炸结果函数*/
aiComplete.rcvResult = function(x, y, r){
	aiComplete.board[x][y] = r;
	if (r == 3){
		var xy = Array(2);
		xy[0] = x;
		xy[1] = y;
		aiComplete.headQueue.push(xy);
	}
}

aiComplete.aiShoot = function(){
	var aim;
	if (aiComplete.mostPossCount>0){
		var index = parseInt(Math.random()*aiComplete.mostPossCount);
		aim = aiComplete.mostPoss[index];
		aiComplete.mostPoss.splice(index, 1);
		aiComplete.mostPossCount--;
	}
	else{
		aim = [parseInt(Math.random()*10),parseInt(Math.random()*10)];
		while (aiComplete.tmpBoard[aim[0]][aim[1]]>0){
			aim = [parseInt(Math.random()*10),parseInt(Math.random()*10)];
		}
		aiComplete.copyTmpBoard();
		for (var i = 0; i < aiComplete.headQueue.length; i++) {
			//console.log(aiComplete.headQueue);
			aiComplete.rmFlight(aiComplete.headQueue[i][0],aiComplete.headQueue[i][1]);
		}
		var startIndex = parseInt(Math.random()*100);
		var maxRate = 0;
		var maxRateD = 0;
		for (var i = startIndex; i < startIndex + 100; i++) {
			var index = i%100;
			var x = index%10;
			var y = parseInt(index/10);
			if (aiComplete.tmpBoard[x][y]>0) continue;
			var tmp = aiComplete.matchRate(x,y);
			if (tmp[0]>maxRate){
				aim = [x,y];
				maxRate = tmp[0];
				maxRateD = tmp[1];
			}
		}
		console.log('ai choose ('+aim[0]+'，'+aim[1]+')'+'('+maxRateD+')'+'matchrate:'+maxRate);
	}
	return aim;
}

//求点(x,y)在地图matchMap上是可能为飞机头的最大匹配度
aiComplete.matchRate = function(x, y){
	var maxRate = 0;
	var maxRateD = 0;
	for (var d = 0; d < 4; d++) {
		var tmpRate = aiComplete.matchRateD(x, y, d);
		if (tmpRate>maxRate){
			maxRate = tmpRate;
			maxRateD = d;
		}
	}
	return [maxRate, maxRateD];
}

//求点(x,y)方向为d在地图matchMap上是可能为飞机头的匹配度
aiComplete.matchRateD = function(x, y, d){
	var rate = 1;
	if ((x<xMargin[0][d])||(x>xMargin[1][d])||(y<yMargin[0][d])||(y>yMargin[1][d])){
		return 0;
	}
	else{
		for (var i = 1; i < 10; i++) {
			var xi = x + dirInc[d][i][0];
			var yi = y + dirInc[d][i][1];
			if (aiComplete.tmpBoard[xi][yi]==2) rate++;
			if ((aiComplete.tmpBoard[xi][yi]==1)||(aiComplete.tmpBoard[xi][yi]==3)) return 0;
		}
		return rate;
	}
}
//去掉匹配度最大的飞机
aiComplete.rmFlight = function(x, y){
	var maxRate = 0;
	var maxRateD = 0;
	//获取最大匹配度飞机
	for (var d = 0; d < 4; d++) {
		var tmpRate = aiComplete.matchRateD(x, y, d);
		//console.log('tmpRate:'+tmpRate+' d:'+d);
		if (tmpRate>maxRate){
			maxRate = tmpRate;
			maxRateD = d;
		}
	}
	//aiComplete.printBoard();
	console.log('remove flight> ('+x+','+y+') maxRate:'+maxRate+' maxRateD:'+maxRateD);
	for (var i = 1; i < 10; i++) {
		var xi = x + dirInc[maxRateD][i][0];
		var yi = y + dirInc[maxRateD][i][1];
		aiComplete.tmpBoard[xi][yi] = 1;
	}
	//aiComplete.printBoard();
}

aiComplete.printBoard = function(){
	console.log('board:');
	for (var i = 0; i < 10; i++) {
		console.log(aiComplete.board[i]);
	}
	console.log('tmpBoard:');
	for (var i = 0; i < 10; i++) {
		console.log(aiComplete.tmpBoard[i]);
	}
}

aiComplete.copyTmpBoard = function(){
	for (var i = 0; i < 10; i++) {
		aiComplete.tmpBoard[i] = aiComplete.board[i].concat();
	}
}
